package com.example.ibcacontactcard;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

@SuppressLint({ "SimpleDateFormat", "SdCardPath" }) public class ContactInfo extends Activity {
	
	Button mButton;
	EditText mName;
	EditText mEmail;
	EditText mPhone;
	EditText mComments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_info);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.contact_info, menu);
        return true;
    }
    
    public void uploadContact(View view){
    	Date today = Calendar.getInstance().getTime();
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    	File FILENAME = new File("/sdcard/", "IBCA");
    	String file = "Contacts-" + formatter.format(today) + ".csv";
    	
    	if (!FILENAME.exists()) {
            FILENAME.mkdirs();
        }
    	
    	EditText userName = (EditText)findViewById(R.id.EnterName);
    	EditText userEmail = (EditText)findViewById(R.id.EnterEmail);
    	EditText userPhone = (EditText)findViewById(R.id.EnterPhone);
    	EditText userComments = (EditText)findViewById(R.id.EnterComments);
    	
    	String name = userName.getText().toString();
    	String email = userEmail.getText().toString();
    	String phone = userPhone.getText().toString();
    	String comments = userComments.getText().toString();
    	
    	String string = name + "," + email + "," + phone + "," + comments + "\n";

    	FileOutputStream fos;;
		try {
			File nfile = new File(FILENAME, file);
			fos = new FileOutputStream(nfile, true);
			
			fos.write(string.getBytes());
			fos.close();
		} catch (IOException e) {
			Log.e("saveToExternalStorage()", FILENAME + " " + e.getMessage());
		}
		
		userName.setText("");
		userEmail.setText("");
		userPhone.setText("");
		userComments.setText("");
    }
    
}
